package com.fr.cell.core.date;

import com.fr.base.FRContext;
import com.fr.cell.core.ModLineBorder;
import com.fr.cell.core.layout.LayoutFactory;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.EventListenerList;

public class JCalendarPanel extends JPanel
{
  private static Border selectedBorder = new LineBorder(Color.black);
  private static Border unselectedBorder = new EmptyBorder(selectedBorder.getBorderInsets(new JLabel()));
  private Calendar calendar;
  private JLabel monthLabel;
  private JPanel days;
  private MouseListener dayBttListener;
  private boolean isSupportDateChangeListener;
  protected Color selectedBackground;
  protected Color selectedForeground;
  protected Color background;
  protected Color foreground;
  private Date selectedDate;
  final SimpleDateFormat monthFormat;
  protected EventListenerList listenerList;

  public JCalendarPanel()
  {
    this(new Date());
  }

  public JCalendarPanel(Date paramDate)
  {
    this.calendar = null;
    this.monthLabel = null;
    this.days = null;
    this.dayBttListener = null;
    this.isSupportDateChangeListener = false;
    this.selectedDate = null;
    this.monthFormat = new SimpleDateFormat("yyyy-MM");
    this.listenerList = new EventListenerList();
    this.selectedDate = paramDate;
    this.calendar = Calendar.getInstance();
    this.selectedBackground = UIManager.getColor("ComboBox.selectionBackground");
    this.selectedForeground = UIManager.getColor("ComboBox.selectionForeground");
    this.background = UIManager.getColor("ComboBox.background");
    this.foreground = UIManager.getColor("ComboBox.foreground");
    this.dayBttListener = createDayBttListener();
    JPanel localJPanel1 = new JPanel();
    localJPanel1.setLayout(new BoxLayout(localJPanel1, 0));
    localJPanel1.setBackground(new Color(0, 0, 128));
    localJPanel1.setForeground(Color.white);
    localJPanel1.setPreferredSize(new Dimension(1, 25));
    JLabel localJLabel = createSkipButton(1, -1);
    localJLabel.setText("<<");
    localJPanel1.add(Box.createHorizontalStrut(12));
    localJPanel1.add(localJLabel);
    localJPanel1.add(Box.createHorizontalStrut(12));
    localJLabel = createSkipButton(2, -1);
    localJLabel.setText("< ");
    localJPanel1.add(localJLabel);
    this.monthLabel = new JLabel("", 0);
    this.monthLabel.setBackground(new Color(0, 0, 128));
    this.monthLabel.setForeground(Color.white);
    localJPanel1.add(Box.createHorizontalGlue());
    localJPanel1.add(this.monthLabel);
    localJPanel1.add(Box.createHorizontalGlue());
    localJLabel = createSkipButton(2, 1);
    localJLabel.setText(" >");
    localJPanel1.add(localJLabel);
    localJLabel = createSkipButton(1, 1);
    localJLabel.setText(">>");
    localJPanel1.add(Box.createHorizontalStrut(12));
    localJPanel1.add(localJLabel);
    localJPanel1.add(Box.createHorizontalStrut(12));
    JPanel localJPanel2 = new JPanel(new GridLayout(0, 7));
    localJPanel2.setBackground(this.background);
    localJPanel2.setOpaque(true);
    DateFormatSymbols localDateFormatSymbols = new DateFormatSymbols(FRContext.getLocale());
    String[] arrayOfString = localDateFormatSymbols.getShortWeekdays();
    for (int i = 1; i <= 7; ++i)
    {
      localJLabel = new JLabel();
      localJLabel.setHorizontalAlignment(0);
      localJLabel.setForeground(this.foreground);
      localJLabel.setText(arrayOfString[i]);
      localJPanel2.add(localJLabel);
    }
    this.days = new JPanel(new GridLayout(0, 7));
    this.days.setBorder(new ModLineBorder(5, Color.black));
    this.days.setBackground(this.background);
    this.days.setOpaque(true);
    JPanel localJPanel3 = new JPanel(new BorderLayout());
    localJPanel3.setBackground(this.background);
    localJPanel3.setOpaque(true);
    localJPanel3.add(localJPanel2, "North");
    localJPanel3.add(this.days, "Center");
    JDayLabel localJDayLabel = new JDayLabel(new Date(), false);
    localJDayLabel.setForeground(this.foreground);
    localJDayLabel.addMouseListener(this.dayBttListener);
    JPanel localJPanel4 = new JPanel(LayoutFactory.createBorderLayout());
    localJPanel4.setBackground(this.background);
    localJPanel4.setForeground(this.foreground);
    localJPanel4.add(localJDayLabel, "Center");
    setPreferredSize(new Dimension(280, 180));
    setForeground(this.foreground);
    setBackground(this.background);
    setBorder(BorderFactory.createLineBorder(Color.black));
    setLayout(LayoutFactory.createBorderLayout());
    add("North", localJPanel1);
    add("Center", localJPanel3);
    add("South", localJPanel4);
    updateDays();
  }

  protected JLabel createSkipButton(int paramInt1, int paramInt2)
  {
    JLabel localJLabel = new JLabel();
    localJLabel.setBorder(unselectedBorder);
    localJLabel.setBackground(new Color(0, 0, 128));
    localJLabel.setForeground(Color.white);
    localJLabel.setRequestFocusEnabled(false);
    localJLabel.addMouseListener(createSkipListener(paramInt1, paramInt2));
    return localJLabel;
  }

  protected MouseListener createSkipListener(int paramInt1, int paramInt2)
  {
    return new MouseAdapter(this, paramInt1, paramInt2)
    {
      private final int val$field;
      private final int val$amount;
      private final JCalendarPanel this$0;

      public void mouseReleased()
      {
        JCalendarPanel.access$000(this.this$0).add(this.val$field, this.val$amount);
        this.this$0.updateDays();
      }

      public void mouseEntered()
      {
        JComponent localJComponent = (JComponent)paramMouseEvent.getComponent();
        localJComponent.setBorder(JCalendarPanel.access$100());
      }

      public void mouseExited()
      {
        JComponent localJComponent = (JComponent)paramMouseEvent.getComponent();
        localJComponent.setBorder(JCalendarPanel.access$200());
      }
    };
  }

  protected void updateDays()
  {
    this.monthLabel.setText(this.monthFormat.format(this.calendar.getTime()));
    this.days.removeAll();
    Calendar localCalendar1 = Calendar.getInstance();
    localCalendar1.setTime(this.selectedDate);
    Calendar localCalendar2 = (Calendar)this.calendar.clone();
    localCalendar2.set(5, 1);
    int i = localCalendar2.get(7);
    localCalendar2.add(5, -i);
    boolean bool = false;
    for (int j = 0; j < 42; ++j)
    {
      localCalendar2.add(5, 1);
      JDayLabel localJDayLabel = new JDayLabel(localCalendar2.getTime());
      localJDayLabel.setForeground(this.foreground);
      localJDayLabel.addMouseListener(this.dayBttListener);
      if ("1".equals(localJDayLabel.getText()))
        bool = !(bool);
      localJDayLabel.setEnabled(bool);
      if ((localCalendar2.get(1) == localCalendar1.get(1)) && (localCalendar2.get(2) == localCalendar1.get(2)) && (localCalendar2.get(5) == localCalendar1.get(5)))
        localJDayLabel.setBorder(new LineBorder(this.selectedBackground, 1));
      this.days.add(localJDayLabel);
    }
    this.days.validate();
  }

  protected MouseListener createDayBttListener()
  {
    return new MouseAdapter(this)
    {
      private final JCalendarPanel this$0;

      public void mousePressed()
      {
      }

      public void mouseClicked()
      {
      }

      public void mouseReleased()
      {
        JDayLabel localJDayLabel = (JDayLabel)paramMouseEvent.getComponent();
        if (this.this$0.isEnabled())
        {
          localJDayLabel.setOpaque(false);
          localJDayLabel.setBackground(this.this$0.background);
          localJDayLabel.setForeground(this.this$0.foreground);
        }
        JCalendarPanel.access$302(this.this$0, true);
        this.this$0.setSelectedDate(localJDayLabel.getDate());
        JCalendarPanel.access$302(this.this$0, false);
      }

      public void mouseEntered()
      {
        if (this.this$0.isEnabled())
        {
          JComponent localJComponent = (JComponent)paramMouseEvent.getComponent();
          localJComponent.setOpaque(true);
          localJComponent.setBackground(this.this$0.selectedBackground);
          localJComponent.setForeground(this.this$0.selectedForeground);
        }
      }

      public void mouseExited()
      {
        if (this.this$0.isEnabled())
        {
          JComponent localJComponent = (JComponent)paramMouseEvent.getComponent();
          localJComponent.setOpaque(false);
          localJComponent.setBackground(this.this$0.background);
          localJComponent.setForeground(this.this$0.foreground);
        }
      }
    };
  }

  public void addDateChangeListener(ChangeListener paramChangeListener)
  {
    this.listenerList.add(ChangeListener.class, paramChangeListener);
  }

  public void removeDateChangeListener(ChangeListener paramChangeListener)
  {
    this.listenerList.remove(ChangeListener.class, paramChangeListener);
  }

  protected void fireDateChanged(ChangeEvent paramChangeEvent)
  {
    Object[] arrayOfObject = this.listenerList.getListenerList();
    for (int i = arrayOfObject.length - 2; i >= 0; i -= 2)
      if (arrayOfObject[i] == ChangeListener.class)
        ((ChangeListener)arrayOfObject[(i + 1)]).stateChanged(paramChangeEvent);
  }

  public void setSelectedDate(Date paramDate)
  {
    this.selectedDate = paramDate;
    this.calendar.setTime(paramDate);
    updateDays();
    if (this.isSupportDateChangeListener)
      fireDateChanged(new ChangeEvent(paramDate));
  }

  public Date getSelectedDate()
  {
    return this.selectedDate;
  }

  public static void main(String[] paramArrayOfString)
  {
    JFrame localJFrame = new JFrame();
    JCalendarPanel localJCalendarPanel = new JCalendarPanel();
    JTextField localJTextField = new JTextField();
    localJTextField.setPreferredSize(new Dimension(120, 25));
    localJCalendarPanel.addDateChangeListener(new ChangeListener(localJTextField)
    {
      private final JTextField val$field;

      public void stateChanged(ChangeEvent paramChangeEvent)
      {
        Date localDate = (Date)paramChangeEvent.getSource();
        SimpleDateFormat localSimpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        this.val$field.setText(localSimpleDateFormat.format(localDate));
      }
    });
    localJFrame.getContentPane().setLayout(new FlowLayout());
    localJFrame.getContentPane().add(localJTextField);
    localJFrame.getContentPane().add(localJCalendarPanel);
    localJFrame.setVisible(true);
  }

  static Calendar access$000(JCalendarPanel paramJCalendarPanel)
  {
    return paramJCalendarPanel.calendar;
  }

  static Border access$100()
  {
    return selectedBorder;
  }

  static Border access$200()
  {
    return unselectedBorder;
  }

  static boolean access$302(JCalendarPanel paramJCalendarPanel, boolean paramBoolean)
  {
    return (paramJCalendarPanel.isSupportDateChangeListener = paramBoolean);
  }
}